import 'package:flutter/material.dart';

import 'package:flutter_widgets_pc/resources/kq_theme_colors.dart';
import 'package:flutter_widgets_pc/theme/kq_theme_manager.dart';
import 'package:flutter_widgets_pc/widgets/button/kq_icon_button.dart';
import 'package:flutter_widgets_pc/widgets/button/kq_ink_well.dart';
import 'package:flutter_widgets_pc/widgets/titleBar/kq_title_bar.dart';

import '../popMenu/kq_popup_menu.dart';

/// 专用于 [KqHeadBar] 左右两边的按钮
///
/// @author 周卓
///
class KqHeadButton<T> extends StatelessWidget {
  /// 按钮类型，文字或图标，默认图标
  final KqHeadButtonType buttonType;

  /// 图标或文字
  final Widget widget;

  /// 提示文字
  final String? tooltip;

  /// 菜单的背景颜色
  final Color? menuBgColor;

  /// 弹出菜单Builder
  final List<KqPopMenuItemData>? menuData;

  /// 弹出菜单选中事件
  final OnMenuSelectListener? onSelected;

  /// 点击回调，如果使用了[menuData],则不能用这个，而应该用[onSelected]
  final VoidCallback? onTap;

  /// 鼠标右键
  final VoidCallback? onRightTap;

  /// 下拉菜单的elevation, 默认0
  final double elevation;

  /// 是否需要多次点击处理，避免连续点击多次。
  final bool needMultiClickHandle;

  const KqHeadButton(
      {Key? key,
      this.buttonType = KqHeadButtonType.icon,
      required this.widget,
      this.menuBgColor,
      this.tooltip,
      this.elevation = 0,
      this.menuData,
      this.onRightTap,
      this.onSelected,
      this.needMultiClickHandle = true,
      this.onTap})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    var iconSize = 32.0;

    if (buttonType == KqHeadButtonType.text) {
      return Container(
        alignment: Alignment.center,
        child: KqInkWell(
          verticalPadding: 10,
          radius: 4,
          horizontalPadding: 4,
          tooltip: tooltip,
          onTap: onTap,
          onSecondaryTap: onRightTap == null ? null : (details) {
            onRightTap?.call();
          },
          child: DefaultTextStyle(
            style: const TextStyle(
              fontSize: 14,
              color: KqThemeColors.textWhite,
            ),
            child: widget,
          ),
        ),
      );
    }
    if (menuData != null) {
      return SizedBox(
        width: iconSize,
        height: iconSize,
        child: KqPopupMenuButton(
          padding: EdgeInsets.zero,
          splashRadius: iconSize / 2,
          icon: widget,
          tooltip: tooltip,
          elevation: elevation,
          color: menuBgColor ?? KqThemeManager.getDialogConfig().bgColor,
          position: KqPopupMenuPosition.under,
          shape: const RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(5)),
          ),
          itemBuilder: (context) {
            return buildItems(menuData!);
          },
          onSelected: (entity) {
            if (onSelected != null) {
              onSelected!(entity.position ?? -1, entity);
            }
          },
        ),
      );
    }
    return KqIconButton(
      needMultiClickHandle: needMultiClickHandle,
      icon: widget,
      size: iconSize,
      tooltip: tooltip,
      onPressed: onTap,
      onRightPressed: onRightTap,
    );
  }

  static List<KqPopupMenuItem<KqPopMenuItemData>> buildItems(
      List<KqPopMenuItemData> data) {
    int i = 0;
    for (KqPopMenuItemData e in data) {
      e.position = i;
      i++;
    }
    List<KqPopupMenuItem<KqPopMenuItemData>> list = [];
    for (KqPopMenuItemData item in data) {
      list.add(KqPopupMenuItem<KqPopMenuItemData>(
        value: item,
        height: 46,
        padding: const EdgeInsets.symmetric(horizontal: 16),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Visibility(
              visible: (item.imageUrl != null || item.customImage != null) &&
                  item.imagePosition == KqPopMenuImagePosition.left,
              child: IconTheme(
                data: IconThemeData(
                    color: KqThemeManager.getDialogConfig().titleColor ??
                        KqThemeColors.text26,
                    size: 20),
                child: Container(
                  margin: const EdgeInsets.only(right: 8),
                  width: 23,
                  height: 23,
                  child: item.customImage ??
                      Image.asset(
                        item.imageUrl ?? "",
                        colorBlendMode:
                            item.autoIconColor ? BlendMode.srcIn : null,
                        color: item.autoIconColor
                            ? (item.hasPermission
                                ? (KqThemeManager.getButtonConfig()
                                        .headButtonMenuIconColor ??
                                    KqThemeColors.text26)
                                : (KqThemeManager.getButtonConfig()
                                        .headButtonMenuIconColorNoPermission ??
                                    KqThemeColors.textBF))
                            : null,
                      ),
                ),
              ),
            ),
            Text(
              item.title,
              style: TextStyle(
                  fontSize: item.fontSize ??
                      KqThemeManager.getButtonConfig().headButtonMenuFontSize ??
                      14,
                  fontWeight: FontWeight.w400,
                  color: item.hasPermission
                      ? (KqThemeManager.getButtonConfig()
                              .headButtonMenuTextColor ??
                          KqThemeColors.text26)
                      : (KqThemeManager.getButtonConfig()
                              .headButtonMenuTextColorNoPermission ??
                          KqThemeColors.textBF)),
            ),
            Visibility(
              visible: (item.imageUrl != null || item.customImage != null) &&
                  item.imagePosition == KqPopMenuImagePosition.right,
              child: Container(
                margin: const EdgeInsets.only(left: 8),
                width: 23,
                height: 23,
                child: item.customImage ??
                    Image.asset(
                      item.imageUrl ?? "",
                      colorBlendMode:
                          item.autoIconColor ? BlendMode.srcIn : null,
                      color: item.autoIconColor
                          ? (item.hasPermission
                              ? (KqThemeManager.getButtonConfig()
                                      .headButtonMenuIconColor ??
                                  KqThemeColors.text26)
                              : (KqThemeManager.getButtonConfig()
                                      .headButtonMenuIconColorNoPermission ??
                                  KqThemeColors.textBF))
                          : null,
                    ),
              ),
            ),
          ],
        ),
      ));
    }
    return list;
  }
}

/// 菜单选中
typedef OnMenuSelectListener = void Function(
    int position, KqPopMenuItemData entity);

enum KqHeadButtonType {
  /// 按钮类型
  text,

  /// 文字
  icon
}

class KqPopMenuItemData {
  int? position;

  /// 标题
  String title;

  /// 图片路径，不传则不显示图标
  String? imageUrl;

  /// 自定义图片控件，优先级大于[imageUrl]
  Widget? customImage;

  /// 图片位置，默认左边
  KqPopMenuImagePosition imagePosition;

  /// 是否自动改变图标颜色，false则使用图标原本颜色，默认为true
  bool autoIconColor;

  /// 是否有权限，无权限则使用无权限的图标和文字颜色，默认为false
  bool hasPermission;

  /// 字体大小，不传则默认使用主题配置的字体大小
  double? fontSize;

  /// 自定义备用字段
  dynamic extra;

  KqPopMenuItemData(
      {required this.title,
      this.imageUrl,
      this.fontSize,
      this.customImage,
      this.imagePosition = KqPopMenuImagePosition.left,
      this.hasPermission = false,
      this.autoIconColor = true,
      this.extra});
}

enum KqPopMenuImagePosition {
  left,
  right,
}
